<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>9.3. Basic Methods</title>
<link rel="stylesheet" href="dbstyle.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.72.0">
<link rel="start" href="index.html" title="Programmer's Reference Guide">
<link rel="up" href="zend.date.html" title="Chapter 9. Zend_Date">
<link rel="prev" href="zend.date.definition.theory.html" title="9.2. Theory of Operation">
<link rel="next" href="zend.date.overview.html" title="9.4. Zend_Date API Overview">
<link rel="chapter" href="introduction.html" title="Chapter 1. Introduction to Zend Framework">
<link rel="chapter" href="zend.acl.html" title="Chapter 2. Zend_Acl">
<link rel="chapter" href="zend.auth.html" title="Chapter 3. Zend_Auth">
<link rel="chapter" href="zend.cache.html" title="Chapter 4. Zend_Cache">
<link rel="chapter" href="zend.config.html" title="Chapter 5. Zend_Config">
<link rel="chapter" href="zend.console.getopt.html" title="Chapter 6. Zend_Console_Getopt">
<link rel="chapter" href="zend.controller.html" title="Chapter 7. Zend_Controller">
<link rel="chapter" href="zend.currency.html" title="Chapter 8. Zend_Currency">
<link rel="chapter" href="zend.date.html" title="Chapter 9. Zend_Date">
<link rel="chapter" href="zend.db.html" title="Chapter 10. Zend_Db">
<link rel="chapter" href="zend.debug.html" title="Chapter 11. Zend_Debug">
<link rel="chapter" href="zend.dojo.html" title="Chapter 12. Zend_Dojo">
<link rel="chapter" href="zend.dom.html" title="Chapter 13. Zend_Dom">
<link rel="chapter" href="zend.exception.html" title="Chapter 14. Zend_Exception">
<link rel="chapter" href="zend.feed.html" title="Chapter 15. Zend_Feed">
<link rel="chapter" href="zend.filter.html" title="Chapter 16. Zend_Filter">
<link rel="chapter" href="zend.form.html" title="Chapter 17. Zend_Form">
<link rel="chapter" href="zend.gdata.html" title="Chapter 18. Zend_Gdata">
<link rel="chapter" href="zend.http.html" title="Chapter 19. Zend_Http">
<link rel="chapter" href="zend.infocard.html" title="Chapter 20. Zend_InfoCard">
<link rel="chapter" href="zend.json.html" title="Chapter 21. Zend_Json">
<link rel="chapter" href="zend.layout.html" title="Chapter 22. Zend_Layout">
<link rel="chapter" href="zend.ldap.html" title="Chapter 23. Zend_Ldap">
<link rel="chapter" href="zend.loader.html" title="Chapter 24. Zend_Loader">
<link rel="chapter" href="zend.locale.html" title="Chapter 25. Zend_Locale">
<link rel="chapter" href="zend.log.html" title="Chapter 26. Zend_Log">
<link rel="chapter" href="zend.mail.html" title="Chapter 27. Zend_Mail">
<link rel="chapter" href="zend.measure.html" title="Chapter 28. Zend_Measure">
<link rel="chapter" href="zend.memory.html" title="Chapter 29. Zend_Memory">
<link rel="chapter" href="zend.mime.html" title="Chapter 30. Zend_Mime">
<link rel="chapter" href="zend.openid.html" title="Chapter 31. Zend_OpenId">
<link rel="chapter" href="zend.paginator.html" title="Chapter 32. Zend_Paginator">
<link rel="chapter" href="zend.pdf.html" title="Chapter 33. Zend_Pdf">
<link rel="chapter" href="zend.registry.html" title="Chapter 34. Zend_Registry">
<link rel="chapter" href="zend.rest.html" title="Chapter 35. Zend_Rest">
<link rel="chapter" href="zend.search.lucene.html" title="Chapter 36. Zend_Search_Lucene">
<link rel="chapter" href="zend.server.html" title="Chapter 37. Zend_Server">
<link rel="chapter" href="zend.service.html" title="Chapter 38. Zend_Service">
<link rel="chapter" href="zend.session.html" title="Chapter 39. Zend_Session">
<link rel="chapter" href="zend.soap.html" title="Chapter 40. Zend_Soap">
<link rel="chapter" href="zend.test.html" title="Chapter 41. Zend_Test">
<link rel="chapter" href="zend.text.html" title="Chapter 42. Zend_Text">
<link rel="chapter" href="zend.timesync.html" title="Chapter 43. Zend_TimeSync">
<link rel="chapter" href="zend.translate.html" title="Chapter 44. Zend_Translate">
<link rel="chapter" href="zend.uri.html" title="Chapter 45. Zend_Uri">
<link rel="chapter" href="zend.validate.html" title="Chapter 46. Zend_Validate">
<link rel="chapter" href="zend.version.html" title="Chapter 47. Zend_Version">
<link rel="chapter" href="zend.view.html" title="Chapter 48. Zend_View">
<link rel="chapter" href="zend.xmlrpc.html" title="Chapter 49. Zend_XmlRpc">
<link rel="appendix" href="requirements.html" title="Appendix A. Zend Framework Requirements">
<link rel="appendix" href="coding-standard.html" title="Appendix B. Zend Framework Coding Standard for PHP">
<link rel="appendix" href="copyrights.html" title="Appendix C. Copyright Information">
<link rel="index" href="the.index.html" title="Index">
<link rel="subsection" href="zend.date.basic.html#zend.date.basic.creation" title="9.3.1. The current date">
<link rel="subsection" href="zend.date.basic.html#zend.date.basic.functions" title="9.3.2. Zend_Date by Example">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader"><table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">9.3. Basic Methods</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="zend.date.definition.theory.html">Prev</a> </td>
<th width="60%" align="center">Chapter 9. Zend_Date</th>
<td width="20%" align="right"> <a accesskey="n" href="zend.date.overview.html">Next</a>
</td>
</tr>
</table></div>
<div class="sect1" lang="en">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="zend.date.basic"></a>9.3. Basic Methods</h2></div></div></div>
<p>
        The following sections show basic usage of <code class="code">Zend_Date</code> primarily by example. For this manual, "dates"
        always imply a calendar date with a time, even when not explicitly mentioned, and vice-versa. The part not
        specified defaults to an internal representation of "zero". Thus, adding a date having no calendar date and a
        time value of 12 hours to another date consisting only of a calendar date would result in a date having that
        calendar date and a time of "noon".
    </p>
<p>
        Setting only a specific date, with no time part, implies a time set to 00:00:00. Conversely, setting only a
        specific time implies a date internally set to 01.01.1970 plus the number of seconds equal to the elapsed hours,
        minutes, and seconds identified by the time. Normally, people measure things from a starting point, such as the
        year 0 A.D. However, many software systems use the first second of the year 1970 as the starting point, and
        denote times as a timestamp offset counting the number of seconds elapsed from this starting point.
    </p>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="zend.date.basic.creation"></a>9.3.1. The current date</h3></div></div></div>
<p>
            Without any arguments, constructing an instance returns an object in the default locale with the current,
            local date using PHP's <code class="code">time()</code> function to obtain the
            <a href="http://en.wikipedia.org/wiki/Unix_Time" target="_top">UNIX timestamp</a>
            for the object. Make sure your PHP environment has the correct
            <a href="zend.date.html#zend.date.setdefaulttimezone" title="9.1.1. Always Set a Default Timezone">default timezone</a>
            .
        </p>
<div class="example">
<a name="zend.date.basic.creation.example-1"></a><p class="title"><b>Example 9.2. Creating the current date</b></p>
<div class="example-contents"><pre class="programlisting">&lt;?php
require_once 'Zend/Date.php';

$date = new Zend_Date();

// Output of the current timestamp
print $date;
            </pre></div>
</div>
<br class="example-break">
</div>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="zend.date.basic.functions"></a>9.3.2. Zend_Date by Example</h3></div></div></div>
<p>
            Reviewing basic methods of <code class="code">Zend_Date</code> is a good place to start for those unfamiliar with date
            objects in other languages or frameworks. A small example will be provided for each method below.
        </p>
<div class="sect3" lang="en">
<div class="titlepage"><div><div><h4 class="title">
<a name="zend.date.simple.functions.get"></a>9.3.2.1. Output a Date</h4></div></div></div>
<p>
                The date in a <code class="code">Zend_Date</code> object may be obtained as a localized integer or string using the
                <code class="code">get()</code> method. There are many available options, which will be explained in later sections.
            </p>
<div class="example">
<a name="zend.date.simple.functions.get.example-1"></a><p class="title"><b>Example 9.3. get() - output a date</b></p>
<div class="example-contents"><pre class="programlisting">&lt;?php
require_once 'Zend/Date.php';

$date = new Zend_Date();

// Output of the desired date
print $date-&gt;get();
                </pre></div>
</div>
<br class="example-break">
</div>
<div class="sect3" lang="en">
<div class="titlepage"><div><div><h4 class="title">
<a name="zend.date.simple.functions.set"></a>9.3.2.2. Setting a Date</h4></div></div></div>
<p>
                The <code class="code">set()</code> method alters the date stored in the object, and returns the final date value as
                a timestamp (not an object). Again, there are many options which will be explored in later sections.
            </p>
<div class="example">
<a name="zend.date.simple.functions.set.example-1"></a><p class="title"><b>Example 9.4. set() - set a date</b></p>
<div class="example-contents"><pre class="programlisting">&lt;?php
require_once 'Zend/Date.php';

$date = new Zend_Date();

// Setting of a new time
$date-&gt;set('13:00:00',Zend_Date::TIMES);
print $date-&gt;get(Zend_Date::W3C);
                </pre></div>
</div>
<br class="example-break">
</div>
<div class="sect3" lang="en">
<div class="titlepage"><div><div><h4 class="title">
<a name="zend.date.simple.functions.add"></a>9.3.2.3. Adding and Subtracting Dates</h4></div></div></div>
<p>
                Adding two dates with <code class="code">add()</code> usually involves adding a real date in time with an artificial
                timestramp representing a date part, such as 12 hours, as shown in the example below. Both
                <code class="code">add()</code> and <code class="code">sub()</code> use the same set of options as <code class="code">set()</code>, which will
                be explained later.
            </p>
<div class="example">
<a name="zend.date.simple.functions.add.example-1"></a><p class="title"><b>Example 9.5. add() - adding dates</b></p>
<div class="example-contents"><pre class="programlisting">&lt;?php
require_once 'Zend/Date.php';

$date = new Zend_Date();

// changes $date by adding 12 hours
$date-&gt;add('12:00:00', Zend_Date::TIMES);

echo "Date via get() = ", $date-&gt;get(Zend_Date::W3C), "\n";

// use magic __toString() method to call Zend_Date's toString()
echo "Date via toString() = ", $date, "\n";
                </pre></div>
</div>
<br class="example-break">
</div>
<div class="sect3" lang="en">
<div class="titlepage"><div><div><h4 class="title">
<a name="zend.date.simple.functions.compare"></a>9.3.2.4. Comparison of dates</h4></div></div></div>
<p>
                All basic <code class="code">Zend_Date</code> methods can operate on entire dates contained in the objects, or can
                operate on date parts, such as comparing the minutes value in a date to an absolute value. For example,
                the current minutes in the current time may be compared with a specific number of minutes using
                <code class="code">compare()</code>, as in the example below.
            </p>
<div class="example">
<a name="zend.date.simple.functions.compare.example-1"></a><p class="title"><b>Example 9.6. compare() - compare dates</b></p>
<div class="example-contents"><pre class="programlisting">&lt;?php
require_once 'Zend/Date.php';

$date = new Zend_Date();

// Comparation of both times
if ($date-&gt;compare(10, Zend_Date::MINUTE) == -1) {
    print "This hour is less than 10 minutes old";
} else {
    print "This hour is at least 10 minutes old";
}
                </pre></div>
</div>
<br class="example-break"><p>
                For simple equality comparisons, use <code class="code">equals()</code>, which returns a boolean.
            </p>
<div class="example">
<a name="zend.date.simple.functions.compare.example-2"></a><p class="title"><b>Example 9.7. equals() - identify a date or date part</b></p>
<div class="example-contents"><pre class="programlisting">&lt;?php
require_once 'Zend/Date.php';

$date = new Zend_Date();

// Comparation of the two dates
if ($date-&gt;equals(10, Zend_Date::HOUR)) {
    print "It's 10 o'clock. Time to get to work.";
} else {
    print "It is not 10 o'clock. You can keep sleeping.";
}
                </pre></div>
</div>
<br class="example-break">
</div>
</div>
</div>
<div class="navfooter"><table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="zend.date.definition.theory.html">Prev</a> </td>
<td width="20%" align="center"><a accesskey="u" href="zend.date.html">Up</a></td>
<td width="40%" align="right"> <a accesskey="n" href="zend.date.overview.html">Next</a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">9.2. Theory of Operation </td>
<td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td>
<td width="40%" align="right" valign="top"> 9.4. Zend_Date API Overview</td>
</tr>
</table></div>
<div class="revinfo"></div>
</body>
</html>
